package a51qindian.com.comprehensive.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.li.interfaces.OnItemClickListener;
import com.li.utils.TransformUtil;
import com.li.widget.DoubleTextView;

import java.util.List;

import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.fragment.ApplyFragment;
import a51qindian.com.comprehensive.model.ApplyInfo;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: Li
 * Date: 2017/9/22 0022
 * Description: 申请
 */

public class ApplyAdapter extends RecyclerView.Adapter<ApplyAdapter.ApplyHolder> {


    private LayoutInflater mInflater;
    private String mType;
    private List<ApplyInfo> mApplyInfoList;
    public ApplyAdapter(Context context, String type) {
        mInflater = LayoutInflater.from(context);
        mType = type;
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    private OnItemClickListener mItemClickListener;
    @Override
    public ApplyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_apply, parent, false);
        final ApplyHolder holder = new ApplyHolder(view);
        switch (mType) {
            case ApplyFragment.APPLY_UNFINISHED:
                holder.mTxtDealInfo.setVisibility(View.GONE);
                if (mItemClickListener != null) {
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mItemClickListener.onItemClick(v,holder.getLayoutPosition());
                        }
                    });
                }
                break;
            case ApplyFragment.APPLY_FINISHED:
                holder.mTxtDealInfo.setVisibility(View.VISIBLE);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(ApplyHolder holder, int position) {
        ApplyInfo info=mApplyInfoList.get(position);
        holder.mTxtShopName.setTxtRight(info.getName());
        holder.mTxtContact.setTxtRight(info.getLinkman());
        holder.mTxtPhone.setTxtRight(info.getMobile());
        holder.mTxtAddress.setTxtRight(info.getAddress());
        holder.mTxtDealInfo.setTxtRight(info.getOperator());
        holder.mTxtNum.setTxtRight(info.getNumber());
        holder.mTxtTime.setTxtRight(TransformUtil.timeFormat(info.getAddtime()));
        String status="";
        switch (mType) {
            case "0":
                status="未分配";
                break;
            case "1":
                status="已分配";
                break;
        }
        holder.mTxtStatus.setTxtRight(status);
    }

    @Override
    public int getItemCount() {
        return mApplyInfoList==null?0:mApplyInfoList.size();
    }

    public void setApplyInfoList(List<ApplyInfo> applyInfoList) {
        mApplyInfoList = applyInfoList;
        notifyDataSetChanged();
    }

    static class ApplyHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_shop_name)
        DoubleTextView mTxtShopName;
        @BindView(R.id.txt_contact)
        DoubleTextView mTxtContact;
        @BindView(R.id.txt_phone)
        DoubleTextView mTxtPhone;
        @BindView(R.id.txt_address)
        DoubleTextView mTxtAddress;
        @BindView(R.id.txt_deal_info)
        DoubleTextView mTxtDealInfo;
        @BindView(R.id.txt_num)
        DoubleTextView mTxtNum;
        @BindView(R.id.txt_time)
        DoubleTextView mTxtTime;
        @BindView(R.id.txt_status)
        DoubleTextView mTxtStatus;
        public ApplyHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
